﻿//-----------------------------------------------------------------------
// <copyright file="CorporateEmailValidator.cs" company="OverrideThis">
//     Copyright (c) 2010 OverrideThis.com
// </copyright>
//-----------------------------------------------------------------------
namespace OverrideThis.ContactManager.WebSite.Infrastructure.Validators
{
    using System.Collections.Generic;
    using System.Web.Mvc;

    public class CorporateEmailValidator : DataAnnotationsModelValidator<CorporateEmailAttribute>
    {
        private string message;

        public CorporateEmailValidator(ModelMetadata metadata,
                                       ControllerContext context,
                                       CorporateEmailAttribute attribute)
            : base(metadata, context, attribute)
        {
            this.message = attribute.FormatErrorMessage(metadata.PropertyName);
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            var errorRule = new ModelClientValidationRule
            {
                ErrorMessage = this.message,
                ValidationType = "corporateEmail"
            };
            errorRule.ValidationParameters.Add("corporateDomain", base.Attribute.CorporateEmail);
            return new[] { errorRule };
        }
    }
}